<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <title>增加商户信息</title>
        
        <link rel="stylesheet" type="text/css" href="../css/cm-default.css"  />
        <link rel="stylesheet" type="text/css" href="../ligerUI/skins/Aqua/css/ligerui-all.css" />
        <link rel="stylesheet" type="text/css" href="../ligerUI/skins/ligerui-icons.css" />
        <script src="../js/jquery-min.js" type="text/javascript"></script>
        <script src="../ligerUI/js/ligerui.min.js" type="text/javascript"></script>
        <script src="../js/common.js" type="text/javascript"></script>
        <script src="../js/json2.js" type="text/javascript"></script>
        <script type="text/javascript" src="../js/md5.js"></script>
        <script src="../js/validate/jquery.validate.js" type="text/javascript"></script>
        <script src="../js/validate/jquery.metadata.js" type="text/javascript"></script>
        <script src="../js/validate/messages_zh.js" type="text/javascript"></script>
        <script type="text/javascript">
             var opt,c_code; 
            $(function(){
                var query = getQueryString(window.location.toString());
                opt = query.o;
                if (opt) {
                    c_code = query.code;
                   
                    if (opt === 'm') {
                    	 $("#passwdLab").hide();
                        var param = {shop_no:c_code};
                        var data = JSON.stringify(reqObj('q','wifi.select.shop_basic_info.query2',JSON.stringify(param)));
                        ajaxSubmit("/cmwifi/rbac/cm.do?m=c", data, function(data) {
                            bindVal(data['data']);
                        }, null, false);
                        
                        ajaxSubmit("/cmwifi/rbac/cmUser.do?method=getUserInfo", null, function(data) {
                            $('#WriteUserCodeS').val(data['uid']);
                            $("#WriteDateS").val(data['date']);
                        }, null, false);
                    }
                } else {
                    $("#passwdLab").show();
                    //初始化签订日期
                    $("#agreed_dateS").val(getToDay());
                    ajaxSubmit("/cmwifi/rbac/cmUser.do?method=getUserInfo", null, function(data) {
                        $('#createUserS').val(data['uname']);
                        $('#createUserCodeS').val(data['uid']);
                        $("#createDateS").val(data['date']);
                    }, null, false);
                }             
                $("#pageloading").hide();
                });
           //保存处理中值check处理
           function validate() {
               var valid = true;
               $('*[id$="S"],*[id$="S_txt"]').each(function(index, element) {
                   if (!$(this).rules()) return;
                   if (!$(element).valid()) {
                       valid = false;
                       return;
                   }
               });
               if (!valid) return false;
               if(opt != 'm'){
               var shop_no = $('#shop_noS').val();
               var param = {shop_no:shop_no};
               var count = 0;
               data = JSON.stringify(reqObj('q','wifi.select.shop_num.query',JSON.stringify(param)));
               ajaxSubmit("/cmwifi/rbac/cm.do?m=c", data, function(data) {
                   count = data['data'].Rows[0].count_num;
               }, null, false);
               if(count > 0){
                   $.ligerDialog.error('此商户账号已经注册！', '数据错误');
                   count = 0 ;
                   return false;
               }
               }
               return true;
           }

           // 查询时数据值绑定  应该可以抽象为公共的功能
           function bindVal(data) {
               if (data.Rows && data.Rows.length === 1) {
                   data = data.Rows[0];
                   $('#shop_nameS').val(data.shop_name);
                   $('#shop_noS').val(data.shop_no);
                   $('#shop_pwdS').val(data.shop_pwd);
                   $('#agreed_noS').val(data.agreed_no);
                   $('#agreed_dateS').val(data.agreed_date);
                   $('#agreed_termS').val(data.agreed_term);                 
                   $('#shop_termS').val(data.shop_term);
                   $('#shop_noteS').val(data.shop_note);
                   $('#shop_urlS').val(data.shop_url);
                   $('#shop_addressS').val(data.shop_address);
                   $('#createUserS').val(paramRender(null,'user',data.createuser));
                   $('#createUserCodeS').val(data.createuser);
                   $('#createDateS').val(data.createdate);
               }
           }
           // 保存业务处理
           function toAdd() {
              if (!validate())
                  return;
               var param = {
                            shop_name:$("#shop_nameS").val(),
                            agreed_no:$("#agreed_noS").val(),
                            agreed_date:$("#agreed_dateS").val(),
                            agreed_term:$("#agreed_termS").val(),
                            shop_term:$("#shop_termS").val(),
                            shop_note:$("#shop_noteS").val(),
                            shop_no:$('#shop_noS').val(),
                            shop_url:$('#shop_urlS').val(),
                            shop_address:$('#shop_addressS').val()
               };
               var en = 'wifi.add.shop_basic_info.add', optType = 'a';
               if (opt === 'm') {
                   
                   //修改人
                   param.writeuser=$('#WriteUserCodeS').val();
                   //修改日期
                   param.writedate=$('#WriteDateS').val();
                   // 进行修改数据库处理
                   en = 'wifi.update.shop_basic_info.update';
                   // 操作类型为 update
                   optType = 'u';
               } else {
                   // 新增时 数据处理
                   param.createuser = $("#createUserCodeS").val();
                   param.createdate = $("#createDateS").val();
                   param.shop_useable="1";
                   param.shop_pwd=hex_md5($('#shop_pwdS').val()).toUpperCase();
               }
               var data = JSON.stringify(reqObj(optType,en,JSON.stringify(param)));
               ajaxSubmit("/cmwifi/rbac/cm.do?m=c", data, function(data) {
                    $.ligerDialog.success('保存成功', '操作完成',closeDialog);
               });
           }
           //重新加载页面 ，在保存时调用
            function closeDialog() {
            	if (opt === 'm') {
            		 parent.modifyDialog.hide();
            	}else{
            		 parent.addDialog.hide();
            	}
            	parent.toQuery();//调父窗口某个方法
               }
       </script>
       <style type="text/css">
           .center-height{height: 100%;}
       </style>
   </head>
   <body style="padding:0;">
       <div id="pageloading"></div>
       <div id="layout_main">
           <div position="center" id="a_center" title="商户基本信息" style="overflow:auto;">
               <form name="basic-form" method="post" id="basic_form" class="liger-form" data-validate="{}">
                   <div class="line-div">
                       <div class="label-div"><label>商户名称</label></div>
                       <div class="in-div"><input type="text" id="shop_nameS" ltype="text" class="required diff" /></div>
                       <div class="label-div"><label>商户账号</label></div>
                       <div class="in-div"><input type="text" id="shop_noS" ltype="text" class="required diff"/>
                       </div>
                   </div>
                   <div class="line-div">
                       <div class="label-div"> <label>商户地址</label></div>
                       <div class="in-div"><input type="text" id="shop_addressS" ltype="text" class="required diff"/></div>
                       <div class="label-div"> <label>商户网站</label></div>
                       <div class="in-div"><input type="text" id="shop_urlS" ltype="text" class="diff"/></div>
                   </div>
                   <div class="line-div">
                      <div class="label-div"><label>wifi共享协议编号</label></div>
                       <div class="in-div"><input type="text" id="agreed_noS" ltype="text" class="required diff"/></div>
                       <input type="hidden" id="partner_idS" />
                       <div class="label-div"><label>协议签订日期</label></div>
                       <div class="in-div"><input type="text" id="agreed_dateS" ltype="date" class="required diff" /></div>
                   </div>
                   <div class="line-div">
                       <div class="label-div"><label>协议期</label></div>
                       <div class="in-div"><input type="text" id="agreed_termS" ltype="date" class="required diff"/></div>
                       <div class="label-div"><label>账号有效期</label></div>
                       <div class="in-div"><input type="text" id="shop_termS" ltype="date" class="required diff" /></div>
                   </div>
                   <div class="line-div" id="passwdLab">
                       <div class="label-div"><label>账号密码</label></div>
                       <div class="in-div"><input type="password" id="shop_pwdS" ltype="text" class="required diff"/>
                       </div>
                   </div>
                   <div class="line-div" style="height:76px;">
                       <div class="label-div"><label>备注说明</label></div>
                       <div class="in-div" style="width:72%;">
                           <textarea cols="80" rows="2" id="shop_noteS"></textarea>
                       </div>
                   </div>
                   <div class="line-div">
                       <div class="label-div"><label>创建人</label></div>
                       <div class="in-div"><input type="text" id="createUserS" ltype="text" class="readonly diff" readonly="readonly" /></div>
                       <div class="label-div"><label>创建时间</label></div>
                       <div class="in-div"><input type="text" id="createDateS" ltype="text" class="readonly diff" readonly="readonly" /></div>
                       <input type="hidden" id="createUserCodeS" />
                       <input type="hidden" id="WriteUserCodeS" />
                       <input type="hidden" id="WriteDateS" />
                   </div>
               </form>
           </div>
            <div class="line-div" style="margin-top:15px;padding-left:40%;">
                <input type="button" value="保存" onclick="toAdd();" />
                <input type="button" value="清空" onclick="toReset('#a_center input,select,textarea,date')" />
            </div>
       </div>
   </body>
</html>